"""
密钥管理模型
创建时间：2025-09-12
作者：开发团队
目的：定义密钥数据模型
"""

from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime, timezone
from ..core.database import Base


class Key(Base):
    """密钥模型"""
    
    __tablename__ = "keys"
    
    # 基础字段
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    content = Column(String(255), nullable=False)
    status = Column(String(20), default="active", nullable=False, index=True)  # active, expired, archived
    created_by = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), nullable=False)
    expired_at = Column(DateTime, index=True)
    
    # 关系
    creator = relationship("User", back_populates="keys")
    
    def __repr__(self):
        return f"<Key(id={self.id}, status='{self.status}')>"